package com.huc.ribbon.rest.init;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RibbonInit {

	@Autowired
	private DiscoveryClient discoveryClient;

	/**
	 * 初始化权重的信息配置在redis中，初始化的时候将配置放入内存中
	 */
	@Bean
	public void init() {
		System.out.println("初始化负载均衡权重配置......");
		List<ServiceInstance> orderLists = discoveryClient.getInstances("springboot-ribbon-order");
		for (ServiceInstance serviceInstance : orderLists) {
			serviceInstance.getMetadata().put("weight", "100");
		}

		List<ServiceInstance> userLists = discoveryClient.getInstances("springboot-ribbon-user");
		for (ServiceInstance serviceInstance : userLists) {
			serviceInstance.getMetadata().put("weight", "100");
		}
	}

}
